package cn.com.fssg.platform.base.handler.impl.pjqc;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 评价期次状态更新接口
 */
@Component
public class PjqcStatusCtl implements InterfaceHandler {

//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        // 评价期次id
        String qcid = jsonObject.get("id").toString();
        String zt = jsonObject.get("zt").toString();
        String sql = "UPDATE bs_pjqc SET zt=:zt WHERE id=:id";
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", qcid);
        params.put("zt", zt);
        // 更新状态
        jdbcTemplate.update(sql, params);
        if ("停止录入".equals(zt)) {
            // 停止录入需要新生成下一年评价期次
            sql = "select * from bs_pjqc where id=:id";
            params.remove("zt");
            List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql, params);
            Map<String, Object> insertMap = queryForList.get(0);
            insertMap.put("id", BizUtil.cur().getSnowflakeIdStr());
            insertMap.put("zt", "初始化");
            Date pjkssj = (Date) insertMap.get("pjkssj");
            // 时间增加一年
            int nextYear = DateUtil.year(pjkssj) + 1;
            String pjqc = nextYear + "年" + insertMap.get("pjff") + "评价";
            insertMap.put("pjqc", pjqc);
            insertMap.put("pjkssj", DateUtil.offset(pjkssj, DateField.YEAR, 1));
            Date pjjssj = (Date) insertMap.get("pjjssj");
            insertMap.put("pjjssj", DateUtil.offset(pjjssj, DateField.YEAR, 1));
            Date sskssj = (Date) insertMap.get("sskssj");
            insertMap.put("sskssj", DateUtil.offset(sskssj, DateField.YEAR, 1));
            Date ssjssj = (Date) insertMap.get("ssjssj");
            insertMap.put("ssjssj", DateUtil.offset(ssjssj, DateField.YEAR, 1));
            sql = "insert into bs_pjqc(id,zt,pjqc,pjkssj,pjjssj,sskssj,ssjssj,pjff,pjlb,bz) values (:id,:zt,:pjqc,:pjkssj,:pjjssj,:sskssj,:ssjssj,:pjff,:pjlb,:bz)";
            jdbcTemplate.update(sql, insertMap);
        }
        return null;
    }

}
